package com.example.demo.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.ExamExam;
import com.example.demo.entity.ExamGroup;
import com.example.demo.entity.ExamUser;
import com.example.demo.entity.ExamUserGroup;
import com.example.demo.service.impl.ExamGroupServiceImpl;
import com.example.demo.service.impl.ExamUserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Component
public class ExamUserController {
    private final ExamUserServiceImpl examUserService;
    private final ExamGroupServiceImpl examGroupService;
    @GetMapping("/selectuser")
    public List<ExamUserGroup> selectuser() {
        List<ExamUser> list =examUserService.list();
        List<ExamUserGroup> l=getGroup(list);
        return l;
    }
    public List<ExamUserGroup> getGroup(List<ExamUser> list){
        List<ExamUserGroup> l=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getIsdelete()==1){
                ExamUserGroup e=new ExamUserGroup();
                e.setId(list.get(i).getId());
                e.setUsername(list.get(i).getUsername());
                e.setCompanyname(list.get(i).getCompanyname());
                e.setIdcard(list.get(i).getIdcard());
                e.setGroup(list.get(i).getGroupid());
                e.setUsersex(list.get(i).getUsersex());
                e.setPhonenumber(list.get(i).getPhonenumber());
                e.setPasswords(list.get(i).getPasswords());
                if(list.get(i).getGroupid()!=null){
                    String[] groupid=list.get(i).getGroupid().split(",");
                    List a=new ArrayList();
                    for (int j = 0; j < groupid.length; j++) {
                        if(!groupid[j].equals("")){
                            ExamGroup examGroup=examGroupService.selectById(Integer.parseInt(groupid[j]));
                            if(examGroup!=null){
                                a.add(examGroup);
                            }
                        }
                    }
                    e.setExamGroupList(a);
                }
                l.add(e);
            }

        }
        return l;
    }
    @PostMapping("/adduser")
    public boolean adduser(@RequestBody ExamUser examUser) {
        boolean bol=examUserService.save(examUser);
        return bol;
    }
    @PostMapping("/adduserlist")
    public boolean adduser(@RequestBody List<ExamUser> examUserList) {
        List<Boolean> bolist=new ArrayList<>();
        for (int i = 0; i < examUserList.size(); i++) {
            boolean bol=examUserService.save(examUserList.get(i));
            bolist.add(bol);
        }
        if (bolist.contains(false)) {
            return false;
        } else {
            return true;
        }
    }
    @PostMapping("/updateuser")
    public boolean updateuser(@RequestBody ExamUser examUser) {
        boolean bol=examUserService.updateById(examUser);
        return bol;
    }

    @GetMapping("/deleteuser")
    public boolean deleteuser(Integer id) {
        ExamUser examUser=new ExamUser();
        examUser.setId(id);
        examUser.setIsdelete(2);
        boolean bol =examUserService.updateById(examUser);
        return bol;
    }
    @GetMapping("/updatepassword")
    public boolean updatePassword(String loginInfo,String password) {
        ExamUser examUser=examUserService.selectByIdCard(loginInfo);
        examUser.setPasswords(password);
        boolean bol=examUserService.updateById(examUser);
        return bol;
    }

    @GetMapping("/login")
    public Map Login(String loginInfo,String password) {
        ExamUser examUser =examUserService.login(loginInfo,password);
        Map map=new HashMap<>();
        if (examUser!=null){
            map.put("code",200);
            map.put("data",examUser);
        }else{
            map.put("code",500);
            map.put("data",null);
        }

        return map;
    }

    @GetMapping("/setgroup")
    public void setgroup(String idList,Integer groupId) {
        String[] list=idList.split(",");
        for (int i = 0; i < list.length; i++) {
            ExamUser examUser=examUserService.selectById(Integer.parseInt(list[i]));
            if(examUser.getGroupid()==null){
                examUser.setGroupid(groupId.toString()+",");
                examUserService.updateById(examUser);
            }
            String[] groupIdList=examUser.getGroupid().split(",");
            boolean contains = Arrays.asList(groupIdList).contains(groupId.toString());
            if (!contains) {
                String nowGroup=examUser.getGroupid()+groupId.toString()+",";
                examUser.setGroupid(nowGroup);
                examUserService.updateById(examUser);
            }
        }
    }
    @GetMapping("/groupuser")
    public List<ExamUserGroup> groupUser(Integer id) {
        List<ExamUser> list=examUserService.selectGroup(id);
        List<ExamUserGroup> l=getGroup(list);
        return l;
    }
}
